import json
from tools.ExcelHandler import ExcelHandler


class FinalWrite:
    def __init__(self, sn_json_file, handler_obj):
        self.sn_json_file = sn_json_file
        self.handler = handler_obj

    def get_sn(self, json_file):
        with open(json_file, 'r', encoding='utf8') as file:
            try:
                data = json.loads(file.read())
                # print(data)
            except Exception as e:
                print(e)
        return data

    def get_mem_sn(self, cpu_mem):
        mem = []
        provider = []
        sn = []
        for key, value in cpu_mem.items():
            mem.append(key)
            provider.append(value['provider'])
            sn.append(value['sn'])
        return [mem, provider, sn]

    def get_disk_sn(self, disk):
        number = []
        type = []
        sn = []
        for k, v in disk.items():
            number.append(v['number'])
            type.append(v['type'])
            sn.append(v['sn'])
        return [number, type, sn]

    def get_raid_card_sn(self, raid):
        type = []
        sn = []
        for k, v in raid.items():
            type.append(v['type'])
            sn.append(v['sn'])
        return [type, sn]

    def get_network_sn(self, network):
        slot = []
        sn = []
        type = []
        for k, v in network.items():
            slot.append(v['slot'])
            sn.append(v['sn'])
            type.append(v['type'])
        return [slot, sn, type]

    def get_power_sn(self, power):
        slot = []
        sn = []
        for k, v in power.items():
            slot.append(v['slot'])
            sn.append(v['sn'])
        return [slot, sn]

    def write_cell(self, lst, info):
        for cell in lst:
            self.handler.write_value(cell, info[lst.index(cell)])

    def run(self):
        sn_data = self.get_sn(self.sn_json_file)
        cpu1_mem_sn = self.get_mem_sn(sn_data['cpu1_mem'])
        cpu2_mem_sn = self.get_mem_sn(sn_data['cpu2_mem'])
        back_disk_sn = self.get_disk_sn(sn_data['back'])
        front_disk_sn = self.get_disk_sn(sn_data['front'])
        ssd_disk_sn = self.get_disk_sn(sn_data['ssd'])
        raid_card_sn = self.get_raid_card_sn(sn_data['raid_card'])
        network_card_sn = self.get_network_sn(sn_data['network_card'])
        power_sn = self.get_power_sn(sn_data['power'])
        # print(power_sn)

        # 写入服务器信息
        self.handler.write_value('C2', sn_data['服务器型号'])
        self.handler.write_value('C3', sn_data['快速服务代码'])
        self.handler.write_value('C4', sn_data['服务器SN号'])

        # 写入cpu1对应的内存条的信息
        self.handler.clear_cell('D5:Z7')
        c_list = ['D5', 'D6', 'D7']
        self.write_cell(c_list, cpu1_mem_sn)
        # 写入cpu2对应的内存条的信息
        self.handler.clear_cell('D8:Z10')
        c_list = ['D8', 'D9', 'D10']
        self.write_cell(c_list, cpu2_mem_sn)

        # 写入后置硬盘的信息
        self.handler.clear_cell('D11:Z13')
        c_list = ['D11', 'D12', 'D13']
        self.write_cell(c_list, back_disk_sn)
        # 写入后置HDD硬盘信息
        self.handler.clear_cell('D14:Z16')
        c_list = ['D14', 'D15', 'D16']
        self.write_cell(c_list, front_disk_sn)
        # 写入pcid硬盘
        self.handler.clear_cell('D17:Z18')
        self.handler.write_value('D17', 'Slot 4')
        self.handler.write_value('D18', 'Dapu')
        # 写入后置SSD硬盘信息
        self.handler.clear_cell('D19:Z21')
        c_list = ['D19', 'D20', 'D21']
        self.write_cell(c_list, ssd_disk_sn)

        # 写入raid card信息
        self.handler.clear_cell('D22:Z23')
        c_list = ['D22', 'D23']
        self.write_cell(c_list, raid_card_sn)

        # 写入主板和cpu信息
        self.handler.clear_cell('D24:Z25')
        self.handler.write_value('D24', sn_data['main_board_sn'])
        self.handler.write_value('D25', sn_data['cpu_type'])

        # 写入网卡信息
        self.handler.clear_cell('D26:Z28')
        c_list = ['D26', 'D27', 'D28']
        self.write_cell(c_list, network_card_sn)

        # 写入电源信息
        self.handler.clear_cell('D29:Z30')
        c_list = ['D29', 'D30']
        self.write_cell(c_list, power_sn)


if __name__ == '__main__':
    excel_file = '../sn-inventory.xlsx'
    handler = ExcelHandler(excel_file, 'scn02')
    final_write = FinalWrite('../collect/sn.json', handler)
    final_write.run()
